/***
This do-file creates two figures that provide robustness to post-stimulus event 
window for high and low-income. 
***/

*-------------------------------------------------------------------------------
* Set $root 
*-------------------------------------------------------------------------------

project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"

* Create required subfolders
cap mkdir "${root}/results/Policy"

*-------------------------------------------------------------------------------
**# 1. Setup 
*-------------------------------------------------------------------------------
* Set seed 
set seed 1280 

* Load dataset 
project, uses("${root}/data/derived/Policy/stimulus_did_dataset.dta")
use "${root}/data/derived/Policy/stimulus_did_dataset.dta", clear 
gisid round income_q date 

* Empty dataset for us to store the regression estimates 
keep round income_q 
gduplicates drop 

* Dollar effect per $1200 (cumulative over 31 days, varying post-period from 7 to 30 days)
forval post_period = 7/30 {
	gen combined_dollar_rescaled_`post_period' = .
}

sort round income_q

tempfile estimates 
save `estimates', replace

*-------------------------------------------------------------------------------
**# 2. Diff-in-Diff  
*-------------------------------------------------------------------------------

/*
Regression spec: separate regressions for each stimulus round and income quartile 
Dependent variable: spending changes normed to base January 2019 spending, residualized on DoW FEs and detrended
Treatment variables: indicator for post-covid (treat), 
                     interaction between indicator for post-covid and first five days after stimulus/stimulus control (first_five),
					 interaction between indicator for post-covid and subsequent days after stimulus/stimulus control (after_first_five)
*/

forval post_period = 7/30 {
	
	** Redefine the analysis window and post-stimulus window for each post period 
	project, uses("${root}/data/derived/Policy/stimulus_did_dataset.dta")
	use "${root}/data/derived/Policy/stimulus_did_dataset.dta", clear 
	drop analysis_window post first_five after_first_five 
	
	* Define the set of dates in the control and treatment periods 
	gen analysis_window = inrange(date, stim_date - 25, stim_date + `=`post_period' - 1') | inrange(date, stim_date_control - 25, stim_date_control + `=`post_period' - 1') if round == "april"
	replace analysis_window = 0 if inlist(date, mdy(4, 14, 2020), mdy(4, 14, 2019)) & round == "april"

	replace analysis_window = inrange(date, mdy(12, 4, 2020), stim_date + `=`post_period' - 1') | inrange(date, mdy(12, 4, 2019), stim_date_control + `=`post_period' - 1') if round == "january"
	replace analysis_window = 0 if (inrange(date, mdy(12, 15, 2020), mdy(1, 3, 2021)) | inrange(date, mdy(12, 15, 2019), mdy(1, 3, 2020))) & round == "january"

	replace analysis_window = inrange(date, stim_date - 25, stim_date + `=`post_period' - 1') | inrange(date, stim_date_control - 25, stim_date_control + `=`post_period' - 1') if round == "march"
	replace analysis_window = 0 if (inrange(date, mdy(3, 13, 2021), mdy(3, 16, 2021)) | inrange(date, mdy(3, 13, 2019), mdy(3, 16, 2019))) & round == "march"

	* Create post indicator (post-stimulus or post-stimulus control, within the respective years)
	gen post = 1 if (date >= stim_date & year == 2020) & round == "april" & analysis_window == 1
	replace post = 1 if (date >= stim_date_control & year == 2019) & round == "april" & analysis_window == 1
	replace post = 1 if (date >= stim_date & inrange(month, 1, 2) & year == 2021) & round == "january" & analysis_window == 1 
	replace post = 1 if (date >= stim_date_control & inrange(month, 1, 2) & year == 2020) & round == "january" & analysis_window == 1 
	replace post = 1 if (date >= stim_date & year == 2021) & round == "march" & analysis_window == 1
	replace post = 1 if (date >= stim_date_control & year == 2019) & round == "march" & analysis_window == 1
	replace post = 0 if missing(post) & analysis_window == 1 
	assert missing(post) if analysis_window == 0

	* Create indicators for being within the first five days / after first five days of the post-period
	gen first_five = (inrange(date, stim_date, stim_date + 4) | inrange(date, stim_date_control, stim_date_control + 4)) if analysis_window == 1
	gen after_first_five = (first_five == 0 & post == 1) if analysis_window == 1
	assert first_five + after_first_five == post 
	
	
	foreach round in april january march {
		forval income_q = 1/4 {
		
			* Base spending levels per stimulus recipient in Jan 2019
			sum spend_pp if round == "`round'" & income_q == `income_q'
			local spend_pp = r(mean)
		
			* Diff-in-Diff
			reg dow_resid_2019_notrend i.treat##i.first_five i.treat##i.after_first_five if round == "`round'" & income_q == `income_q' & analysis_window == 1, r
		
			* 24 days pre- for April; 11 days pre- for January; 21 days pre- for March
			if "`round'" == "april" assert e(N) == (24 + `post_period') * 2 
			else if "`round'" == "january" assert e(N) == (11 + `post_period') * 2
			else if "`round'" == "march" assert e(N) == (21 + `post_period') * 2 
 		
			* Record regression estimates 
			preserve 
			
			use `estimates', clear 
		
			* Dollar effect (31 days after stimulus, cumulative)
			lincom (1.treat#1.first_five * 5 + 1.treat#1.after_first_five * 26) * `spend_pp'
			
			if "`round'" == "april" replace combined_dollar_rescaled_`post_period' = r(estimate) * 1200 / 1200 if round == "`round'" & income_q == `income_q'
			if "`round'" == "january" replace combined_dollar_rescaled_`post_period' = r(estimate) * 1200 / 600 if round == "`round'" & income_q == `income_q'
			if "`round'" == "march" replace combined_dollar_rescaled_`post_period' = r(estimate) * 1200 / 1400 if round == "`round'" & income_q == `income_q'
	
			save `estimates', replace
			restore
		}
	}
}

*-------------------------------------------------------------------------------
**# 3. Robustness plots 
*-------------------------------------------------------------------------------
foreach income_q in 1 4 {
	
	preserve 

	use `estimates', clear 
	keep if income_q == `income_q'
	gisid round 

	greshape long combined_dollar_rescaled_, i(round) j(post_period)
	rename combined_dollar_rescaled_ combined_dollar_rescaled 

	* Show implied consumption per $1200 on one graph  
	tw ///
	(scatter combined_dollar_rescaled post_period if round == "april", msymbol(o)) /// 
	(scatter combined_dollar_rescaled post_period if round == "january", msymbol(t)) /// 
	(scatter combined_dollar_rescaled post_period if round == "march", msymbol(s)) /// 
		, ///
		xtitle("Number of Days After Stimulus Included in Post-Period") /// 
		ytitle("Estimated 1-Month Spending Impact" "Per $1200 of Stimulus") /// 
		legend(order(1 "April 2020 Stimulus" 2 "January 2021 Stimulus" 3 "March 2021 Stimulus") ring(2) pos(6) cols(3)) /// 
		xlab(7(7)28) ///
		xline(25, lcolor(gs8) lpattern(dash)) ///
		ylab(-200 "-$200" 0 "$0" 200 "$200" 400 "$400" 600 "$600" 800 "$800", nogrid) graphr(margin(zero)) ///
		text(-180 27.5 "25 Day Post-Period", color(gs8) size(small))
		
	oi_graph_export "${root}/results/Policy/Q`income_q' on one plot, varying round and days (dollars)", type(${fig_type}) 
	
	restore
}

*------------------------------------------------------------------------------*
* Output numbers
*------------------------------------------------------------------------------*

* Range of results over choice of post-period, for low income households
use `estimates', clear 
keep if income_q == 1
gisid round 

greshape long combined_dollar_rescaled_, i(round) j(post_period)
rename combined_dollar_rescaled_ combined_dollar_rescaled 

cap erase "${root}/results/paper numbers/Policy/Stimulus Robustness.yaml"

foreach round in april january march {
	
	local round_cap = strproper("`round'")

	sum combined_dollar_rescaled if round == "`round'"

	local robustness_str = cond(r(min) >= 0, "$", "-$") + strofreal(abs(r(min)), "%5.0f") + " to " + ///
							cond(r(max) >= 0, "$", "-$") + strofreal(abs(r(max)), "%5.0f")

	yamlout using "${root}/results/paper numbers/Policy/Stimulus Robustness.yaml", ///
		key("robustness_`round_cap'") ///
		comment("Robustness for Q1 households in `round'") ///
		value(`robustness_str')
}

project, creates("${root}/results/paper numbers/Policy/Stimulus Robustness.yaml")
